package com.smartEdu.commons.system;

import java.util.List;

import com.smartEdu.commons.CMap;
import com.smartEdu.commons.ClassStatic;
import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.po.AcademicPo;
import com.smartEdu.utils.JsonUtil;

import redis.clients.jedis.Jedis;

public class AcademicCommon {
	public static AcademicCommon GetInstance() {
		return Inline._INIT();
	}
	
	/**
	 * 使用静态内部类保证多线程情况下只产生一个单例
	 * @author admin
	 *
	 */
	static class Inline {
		private static final AcademicCommon academic;
		static {
			academic = new AcademicCommon();
		}
		public static AcademicCommon _INIT(){
			return academic;
		}
	}
	
	public void SetRedis(CMap map) {
		Jedis jedis = null;
		try {
			jedis = ClassStatic.redis.getResource();
			AcademicPo academic = (AcademicPo)map.geto("ACADEMIC");
			Object academiclist = map.geto("ACADEMICLIST");
			if(academic != null) {
				jedis.hset(Tag_Tips.SYSTEMPARAMETERMAP, Tag_Tips.ACADEMICKEY, academic.getAcademic());
				jedis.hset(Tag_Tips.SYSTEMPARAMETERMAP, Tag_Tips.ACADEMICKEYLIST, JsonUtil.toJson(academiclist));
			}
			Tag_Tips.ACADEMICVALUE = academic.getAcademic();
		}
		catch (Exception e) {
			LogCvt.error("初始化学期Academic出错",e);
		}
		finally {
    		if (jedis != null) {
    			ClassStatic.redis.returnResource(jedis);
			}
		}
	}
	
	public void GetRedis(Session sess) {
		Jedis jedis = null;
		try {
			jedis = ClassStatic.redis.getResource();
			String academic = jedis.hget(Tag_Tips.SYSTEMPARAMETERMAP, Tag_Tips.ACADEMICKEYLIST);
			if(academic != null) {
				List<AcademicPo> polist = JsonUtil.toListObject(academic, AcademicPo.class);
				sess.put("ACADEMICLIST", polist);
			}
		}
		catch (Exception e) {
			LogCvt.error("从Redis中获取Academic出错",e);
		}
		finally {
    		if (jedis != null) {
    			ClassStatic.redis.returnResource(jedis);
			}
		}
	}
}
